package com.xlh.service.impl;

import com.xlh.dao.UserMessageDetailsMapper;
import com.xlh.dto.user.UserInfoDTO;
import com.xlh.pojo.UserMessageDetails;
import com.xlh.pojo.UserMessageDetailsExample;
import com.xlh.service.SendNewMessageTaskService;
import com.xlh.service.user.UserService;
import com.xlh.websocket.GlobalWebsocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.Future;

/**
 * @author zhr 2019/3/6
 */
@Service
public class SendNewMessageTaskServiceImpl implements SendNewMessageTaskService {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMessageDetailsMapper mapper;

    @Override
    @Async
    public Future<Boolean> asyncSendNewMessage(Long userId, List<UserMessageDetails> detailsList, String content) {
        UserInfoDTO userInfoDTO = userService.getUserInfo(userId, null);
        GlobalWebsocket.sendNewMessage(userId, userInfoDTO.getName(), userInfoDTO.getIcon(), detailsList, content);
        return new AsyncResult<>(true);
    }

    @Override
    @Async
    public Future<Boolean> asyncDeleteMessage(Long userId, List<UserMessageDetails> detailsList) {
        UserInfoDTO userInfoDTO = userService.getUserInfo(userId, null);
        GlobalWebsocket.deleteMessage(userId, userInfoDTO.getName(), detailsList);
        return new AsyncResult<>(true);
    }

    @Override
    @Async
    public Future<Boolean> asyncReadMessage(List<Long> ids) {
        UserMessageDetailsExample example = new UserMessageDetailsExample();
        example.createCriteria().andIdIn(ids);

        UserMessageDetails details = new UserMessageDetails();
        details.setRead(true);
        mapper.updateByExampleSelective(details, example);
        return new AsyncResult<>(true);
    }
}
